<template>
	<view class="content">
		<view class="navbar">
			<view class="nav-item" :class="{current: filterIndex === 0}" @click="tabClick(0)">
				综合排序
			</view>
			<view class="nav-item" :class="{current: filterIndex === 1}" @click="tabClick(1)">
				销量优先
			</view>
			<view class="nav-item" :class="{current: filterIndex === 2}" @click="tabClick(2)">
				<text>价格</text>
				<view class="p-box">
					<text :class="{active: order_by === '+price' && filterIndex === 2}" class="iconfont icon-up"></text>
					<text :class="{active: order_by === '-price' && filterIndex === 2}" class="iconfont icon-down"></text>
				</view>
			</view>
			<text class="cate-item iconfont icon-jifenfenlei" @click="toggleCateMask('show')"></text>
		</view>

		<me-scroll-uni :down="downOption" 
						@down="downCallback" 
						:up="upOption"
						@up="upCallback" 
						@init="mescrollInit"
						bottom="0" top="100">
			<view class="goods-list">
				<view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
					<view class="image-wrapper">
						<image :src="item.images" mode="aspectFill"></image>
					</view>
					<text class="title clamp ellipsis">{{item.title}}</text>
					<view class="price-box">
						<text class="price">{{item.price}}</text>
						<text>已售 {{item.sales_count}}</text>
					</view>
				</view>
			</view>
		</me-scroll-uni>

		<view class="cate-mask" :class="cateMaskState===0 ? 'none' : cateMaskState===1 ? 'show' : ''" @click="toggleCateMask">
			<view class="cate-content" @click.stop.prevent="stopPrevent" @touchmove.stop.prevent="stopPrevent">
				<scroll-view scroll-y class="cate-list">
					<view v-for="item in cateList" :key="item.id">
						<view class="cate-item b-b two">{{item.name}}</view>
						<view v-for="tItem in item.items" :key="tItem.id" class="cate-item b-b three" :class="{active: tItem.id==cateId}"
						 @click="changeCate(tItem)">{{tItem.name}}
						</view>
					</view>
				</scroll-view>
			</view>
		</view>


	</view>
</template>

<script>
	import meScrollUni from "@/components/mescroll-uni/mescroll-uni.vue"
	export default {
		components: {
			meScrollUni,
		},
		data() {
			return {
				cateMaskState: 0, //分类面板展开状态
				headerPosition: "fixed",
				headerTop: "44px",
				filterIndex: 0,
				cateId: 0, //已选三级分类id
				order_by: '+price', //+price 价格从低到高 -price价格从高到低
				cateList: [],
				goodsList: [],
				
				mescroll: null, // mescroll实例对象
				// 下拉刷新的常用配置
				downOption: {
					use: true, //是否启用下拉刷新;默认true
					auto: true //是否在初始化完毕之后自动执行下拉刷新的回调;默认true 
				},
				// 上拉加载的常用配置
				upOption: {
					use: true, //是否启用上拉加载;默认true
					auto: true, //是否在初始化完毕之后自动执行上拉加载的回调,默认true
					page: {
						num: 0, //当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 6 //每页数据的数量，默认10
					},
					noMoreSize: 1, //配置列表的总数量要大于5条才显示‘--END--’的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				curPageData: []
			};
		},

		onLoad(options) {
			
			if(options.hasOwnProperty('tid')){
				this.cateId = options.tid;
			}else if(options.hasOwnProperty('sid')){
				this.cateId = options.sid;
			}else{
				this.cateId = options.fid;
			}
			
			this.loadCateList(options.fid);
		},

		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			if (e.scrollTop >= 0) {
				this.headerPosition = "fixed";
			} else {
				this.headerPosition = "absolute";
			}
		},
		methods: {
			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				this.mescroll = mescroll
			},
			// 下拉刷新的回调
			downCallback(mescroll) {
				setTimeout(function() {
					mescroll.endSuccess();
				}, 1000);
			},
			// 上拉加载的回调
			upCallback(mescroll) {
				// 此时mescroll会携带page的参数：
				let pageNum = mescroll.num; //页码，默认从1开始
				let pageSize = mescroll.size; //页长,默认每页10条
				this.loadData(pageNum, pageSize,mescroll);

			},
			//加载分类
			loadCateList(fid) {
				var self = this;
				var param = {
					data: {
						pid: fid //当前分类对应的一级分类id
					}
				};
				var categoryUrl = this.requestUrl.category;
				this.util.request(categoryUrl, 'post', function(res) {
					if (res.data.code == 0 && res.data.data) {
						self.cateList = res.data.data;
					}
				}, param);
			},
			//加载商品 ，带下拉刷新和上滑加载
			loadData(pageNum = 1, pageSize =6,mescroll = []) {
				var self = this;

				// 如果为第一页，说明起点 为 0
				var start = (pageNum-1)*pageSize;
				var length = pageSize;

				// 获取商品信息的参数
				var param = {
					data: {
						cid: this.cateId,
						m: start, //每次调取的起点
						n: length, //每次调取的数量
						order_by: this.order_by,
					}
				};
				// 获取商品列表信息
				var goodsListUrl = this.requestUrl.categoryGoodsList;
				this.util.request(goodsListUrl, 'post', function(res) {
					if (res.data.code == 0 && res.data.data) {
						self.curPageData = res.data.data;
						self.goodsList = self.goodsList.concat(self.curPageData);
						// 最后一页了，显示end
						mescroll.endSuccess(self.curPageData.length);
					}
				}, param);
			},
			//筛选点击
			tabClick(index) {
				if (this.filterIndex === index && index !== 2) {
					return;
				}
				this.filterIndex = index;

				if (index === 1) {
					this.order_by = '-sales_count';
				}
				if (index === 2) {
					this.order_by = this.order_by === '+price' ? '-price' : '+price';
				}
				this.goodsList = []; // 在这里手动置空列表
				this.mescroll.resetUpScroll(); // 刷新列表数据
			},
			//显示分类面板
			toggleCateMask(type) {
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.cateMaskState = 2;
				setTimeout(() => {
					this.cateMaskState = state;
				}, timer)
			},
			//分类点击
			changeCate(item) {
				this.cateId = item.id;
				this.toggleCateMask();
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.goodsList = [] // 在这里手动置空列表
				this.mescroll.resetUpScroll() // 刷新列表数据
			},
			//详情
			navToDetailPage(item) {
				let id = item.id;
				let cid= item.category_id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}&cid=${cid}`
				})
			},
			stopPrevent() {}
		},
	}
</script>

<style lang="scss" scoped>
	page,
	.content {
		background: $uni-bg-color;
	}

	.navbar {
		position: fixed;
		left: 0;
		/* #ifdef H5 */
		top: var(--window-top);
		/* #endif */
		/* #ifndef H5 */
		top:0;
		/* #endif */
		display: flex;
		width: 100%;
		height: 80upx;
		background: #fff;
		box-shadow: 0 2upx 10upx rgba(0, 0, 0, .06);
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30upx;
			color: $uni-text-color;
			position: relative;

			&.current {
				color: $uni-text-color-zhu;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 120upx;
					height: 0;
					border-bottom: 4upx solid $uni-border-color;
				}
			}
		}

		.p-box {
			display: flex;
			flex-direction: column;

			.iconfont {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 20upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #888;

				&.active {
					color: $uni-text-color-zhu;
				}
			}

			.icon-up:before {
				content: "\e669";
			}

			.icon-down:before {
				content: "\f249";
			}
		}

		.cate-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 44upx;

			&:after {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}

	/* 分类 */
	.cate-mask {
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 95;
		transition: .3s;

		.cate-content {
			width: 500upx;
			height: 100%;
			background: #fff;
			float: right;
			transform: translateX(100%);
			transition: .3s;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.cate-content {
				transform: translateX(0);
			}
		}
	}

	.cate-list {
		display: flex;
		flex-direction: column;
		height: 100%;
		padding-bottom: 30rpx;
		box-sizing: border-box;

		.cate-item {
			display: flex;
			align-items: center;
			height: 90upx;
			padding-left: 30upx;
			font-size: 28upx;
			color: #555;
			position: relative;
		}

		.two {
			height: 64upx;
			color: $uni-text-color-zhu;
			font-size: 30upx;
			background: $uni-bg-color-grey;
			font-weight: bold;
		}

		.three {
			text-indent: 30rpx;
			height: 70rpx;
			;
		}

		.active {
			color: $uni-text-color-zhu;
		}
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;

		.goods-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			
			overflow: hidden;
			image {
				width: 100%;
				height: 100%;
				box-sizing: border-box;
				opacity: 1;
				border-radius: $uni-border-radius-base;
				border:solid 1px $uni-border-color
			}
		}

		.title {
			font-size:$uni-font-size-sm;
			color: $uni-text-color;
			line-height: 80upx;
		}

		.price-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: $uni-text-color;
		}

		.price {
			font-size: $uni-font-size-base;
			color: $uni-text-color-zhu;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 26upx;
			}
		}
	}
</style>
